#
# Ruby Soul - Ruby Embedded Parsing/Lexing using DSL techniques.
#
# Copyright (c) 2008 - Giancarlo Bellido. All Rights Reserved.
#

require 'soul/match/special'

module Soul

  class Match
    def initialize(a_match)
      @match = a_match
    end
  
		def include?(k)
			@match.include? k
		end

		def +@
			OneOrMoreMatch.new(@match)
		end

		# not Match
		def ~@
			NotMatch.new(@match)
		end

		def >>(m2)
			SequenceMatch.new(self, m2)
		end

		def !@
			ZeroOrMoreMatch.new(@match)
		end

		def -@
			ZeroOrOneMatch.new(@match)
		end
	  
  end
  
  class NotMatch < Soul::Match    
    def include?(input)
      return !@match.include?(input)
    end    
  end
  
  class ZeroOrMoreMatch < Soul::Match
  	def include?(input)
			if @match.include? input then
				while @match.include?(input.next); end
			end
			true
		end
	end
  
  class OneOrMoreMatch < Soul::Match
    def include?(input)
    	if @match.include? input then
				while @match.include? input.next; end
				return true
			end
			
			false
		end    
  end
  
  class ZeroOrOneMatch < Soul::Match
    def include? (input)
			input.next if @match.include?(input)
			true
		end
  end
  
  class SequenceMatch < Soul::Match
    def initialize (p1, p2)
      @p1 = p1
      @p2 = p2
    end
    
    def include? (input)
			@p1.include?(input) && @p2.include?(input.next)
    end
  end

	module Matches
		
		def eol
			Soul::Match.new("\n")
		end

		#
		# Char Parsers
		#
		def space
			# return Soul::Parser.new(Soul::Match.new(" \t"))
			" \t"
		end

		def digit
			Soul::Match.new( ('0'..'9') )
		end

	end

end
